﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Collections.ObjectModel;
using RegExpressModel;
using RegExpressModel.Entities;
using RegExpressViewModel.Commands;


namespace RegExpressViewModel
{
	public partial class MultiMatchViewModel
	{
		private MultiMatchModel _model;



		internal MultiMatchViewModel() : this( new MultiMatchModel() )
		{
		}


		internal MultiMatchViewModel(MultiMatchModel model)
		{
			_model = model;
			BaseModel = _model;
			LoadCases();
			MatchAllCmd = new ActionCommand(MatchAll);
			MatchCaseCmd = new ActionCommand<int>(MatchCase);
			AddCaseCmd = new ActionCommand(AddCase);
		}



		private void LoadCases()
		{
			Cases = new ObservableCollection<SingleMatchCaseViewModel>();
			foreach (SingleMatchCase currentCase in _model.MatchCases)
			{
				Cases.Add(new SingleMatchCaseViewModel(currentCase) {Parent = this});
			}
		}



		private void MatchAll()
		{
            _model.PerformMatch();
			foreach (SingleMatchCaseViewModel currentCase in Cases)
			{
				currentCase.Refresh();
			}
		}


		private void MatchCase(int index)
		{
			if(_model.PerformMatch(index))
				Cases[index].Refresh();
		}



		private void AddCase()
		{
			SingleMatchCase newCase = _model.AddMatchCase();

			//Reflect change in ViewModel
			Cases.Add(new SingleMatchCaseViewModel(newCase) {Parent = this});
		}


	}
}
